package com.gxan.service.impl;

import com.gxan.common.JsonResult;
import com.gxan.common.R;

import com.gxan.dao.ResumeDao;
import com.gxan.dao.impl.ResumeDaoImpl;
import com.gxan.pojo.RegistrationResults;
import com.gxan.pojo.Resume;
import com.gxan.service.ResumeService;

import java.sql.SQLException;
import java.util.List;

public class ResumeServiceImpl implements ResumeService {
    private static ResumeDao resumeDao = new ResumeDaoImpl();
    @Override
    public R AddResume(Resume resume) {
        //创建返回前端需要的封装类
        R<Object> objectR = new R<>();


        int i = 0;
        try {
            i = resumeDao.addResume(resume);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i>0){
            objectR.setCode(200);
            objectR.setMsg("添加成功");
        }else{
            objectR.setCode(500);
            objectR.setMsg("添加失败");
        }
        return objectR;
    }

    @Override
    public JsonResult queryResumeList(Integer currentPage, Integer pageSize, Resume resume) {
        //创建返回的对象JsonResult
        JsonResult<List<Resume>> jsonResult = new JsonResult<>();
        //查询需要的JsonResult内容
        //查询总条数和list<Resume>
        try {
            Integer integer = resumeDao.countResume(resume);
            List<Resume> resumes = resumeDao.queryResumeList((currentPage-1)*pageSize, pageSize, resume);
            jsonResult.setCode(0);
            jsonResult.setCount(integer);
            jsonResult.setMsg("查询职位发布成功");
            jsonResult.setData(resumes);

        } catch (SQLException e) {
            jsonResult.setCode(500);
            jsonResult.setMsg("服务器出问题");
            e.printStackTrace();
        }
        return jsonResult;
    }

    @Override
    public R delResumeById(String[] ids) {
        //创建返回的对象R
        R<Object> objectR = new R<>();
        //添加R需要的数据
        for (String id : ids) {
            try {
                int i = resumeDao.delResumeById(id);
                if(i>0){
                    objectR.setCode(200);
                    objectR.setMsg("删除成功");
                }
            } catch (SQLException e) {
                objectR.setCode(500);
                objectR.setMsg("服务器问题");
                e.printStackTrace();
            }
        }

        return objectR;
    }

    @Override
    public R updateResume(Resume resume) {
        //创建返回前端需要的封装类
        R<Object> objectR = new R<>();
        int i = -1;
        try {
            i = resumeDao.updateResume(resume);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i>=0){
            objectR.setCode(200);
            objectR.setMsg("添加成功");
        }else{
            objectR.setCode(500);
            objectR.setMsg("添加失败");
        }
        return objectR;
    }

    @Override
    public R queryRegistrationResultsById(Integer id) {
        //创建返回前端需要的封装类
        R<RegistrationResults> objectR = new R<RegistrationResults>();

        RegistrationResults registrationResults =null;
        try {
             registrationResults = resumeDao.queryRegistrationResultsById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(registrationResults!=null){
            objectR.setCode(200);
            objectR.setMsg("查询成功");
            objectR.setData(registrationResults);
        }else{
            objectR.setCode(500);
            objectR.setMsg("查询失败");
        }
        return objectR;

    }

    @Override
    public R updateRegistrationResults(RegistrationResults registrationResults) {
        //创建返回前端需要的封装类
        R<Object> objectR = new R<>();
        int i = -1;
        try {
            resumeDao.updateResumeById(registrationResults.getRecommend_the_interview(),registrationResults.getResume_id());
            i = resumeDao.updateRegistrationResults(registrationResults);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i>=0){
            objectR.setCode(200);
            objectR.setMsg("添加成功");
        }else{
            objectR.setCode(500);
            objectR.setMsg("添加失败");
        }
        return objectR;
    }

    @Override
    public R employedRegistrationResultsById(Integer resume_id) {
        //创建返回前端需要的封装类
        R<Object> objectR = new R<>();
        int i = -1;
        try {

            i = resumeDao.employedRegistrationResultsById(resume_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i>=0){
            objectR.setCode(200);
            objectR.setMsg("添加成功");
        }else{
            objectR.setCode(500);
            objectR.setMsg("添加失败");
        }
        return objectR;
    }
}
